import { GameConfigData, GameData } from "../../common/GameConfig";
import { fishTypeView } from "./pfSelecFish";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
  @property(cc.Sprite)
  spFish: cc.Sprite = null;
  @property(cc.Label)
  txtFishName: cc.Label = null;
  @property(cc.Label)
  txtMoney: cc.Label = null;
  @property(cc.Node)
  nodeLock: cc.Node = null;
  @property(cc.Prefab)
  pfUnlock: cc.Prefab = null;
  _callback = null;
  _ID = 0;

  setViewData(data) {
    console.assert(data, "data is null");
    if (!data) return;

    this.txtFishName.string = data.fishConfig.name;
    this.txtMoney.string = data.fishConfig.money;
    this._ID = data.fishConfig.id;
    this.spFish.spriteFrame = data.spFish;
    if (data.callback) this._callback = data.callback;
    if (GameData.fish_list.indexOf(data.fishConfig.id) == -1) {
      this.nodeLock.active = true;
    } else {
      this.nodeLock.active = false;
    }
  }

  onButtonItemCallback() {
    if (this.nodeLock.active) 
    {
      let node = cc.instantiate(this.pfUnlock);
      let item = node.getComponent(node.name);
      fishTypeView.node.addChild(node);
      //@ts-ignore
      GameConfigData.fishTimeConfig[this._ID - 1].spFish = fishTypeView.fishArr[this._ID - 1];
      item.setViewData(GameConfigData.fishTimeConfig[this._ID - 1]);
      console.log("show unlock view");
    } 
    else 
    {
      let data = {
        state: true,
        fishType:this._ID,
      }
      if (this._callback) this._callback(data);
      fishTypeView.onButtonCloseCallback();
    }
  }
}
